package com.aishua.andpaydemo.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aishua.andpaydemo.R;
import com.aishua.andpaydemo.activity.ChengcheActivity;
import com.aishua.andpaydemo.activity.KaquanbaoActivity;
import com.aishua.andpaydemo.activity.MessageActivity;
import com.aishua.andpaydemo.adapter.BannerAdapter;
import com.aishua.andpaydemo.adapter.Extensionarea1Adapter;
import com.aishua.andpaydemo.adapter.Extensionarea2Adapter;
import com.aishua.andpaydemo.adapter.HomeHotAdapter;
import com.aishua.andpaydemo.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by aishua on 2018/5/15.
 */

public class HomeFragment extends BaseFragment implements View.OnClickListener {

    private TextView tv_title;
    private TextView tv_setting;
    private ImageView img_title_xiaoxi;
    private Button rb_cardbag;
    private RecyclerView rl_hot;
    private RecyclerView rl_extensionarea1;
    private RecyclerView rl_extensionarea2;
    private ViewPager vp_advertisement;
    private String[] hotname;
    private String[] hotname1;
    private Drawable[] hotname1bg;
    private Drawable[] hotname2bg;
    private String[] hotname2;
    private Drawable[] hotbg;
    private String[] hotbg1;
    private String[] hotbg2;
    private Context mContext;
    private List mlist;
    private ImageView[] imgs;
    //轮播控制
    private Handler handler;
    private Timer timer;
    private TimerTask task;
    private int mPosition;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        mContext = getContext();
        View view = View.inflate(getContext(), R.layout.fragment_home, null);
        initTitleView(view);
        initViewData();
        initextensionarea();
        return view;

    }

    private void initTitleView(View view) {

        tv_title = view.findViewById(R.id.tv_title);
        tv_setting = view.findViewById(R.id.tv_setting);
        rl_hot = view.findViewById(R.id.rl_hot);
        rb_cardbag = view.findViewById(R.id.rb_cardbag);
        tv_setting.setVisibility(View.INVISIBLE);
        vp_advertisement = view.findViewById(R.id.vp_advertisement);
        point_container = view.findViewById(R.id.point_container);
        rl_extensionarea1 = view.findViewById(R.id.rl_extensionarea1);
        rl_extensionarea2 = view.findViewById(R.id.rl_extensionarea2);
        img_title_xiaoxi = view.findViewById(R.id.img_title_xiaoxi);
        img_title_xiaoxi.setOnClickListener(this);
        rb_cardbag.setOnClickListener(this);
    }

    private void initViewData() {

        tv_title.setText("和包支付");
        hotname = new String[]{"充话费", "生活缴费", "移动厅", "和包券", "发红包", "转账", "和聚宝", "和包贷", "乘车码", "共享出行", "共享充电宝", "全部"};

        hotbg = new Drawable[]{getResources().getDrawable(R.drawable.chonghuafei), getResources().getDrawable(R.drawable.shenghuojioafei), getResources().getDrawable(R.drawable.yidongting), getResources().getDrawable(R.drawable.hebaoquan), getResources().getDrawable(R.drawable.fahongbao), getResources().getDrawable(R.drawable.zhuangzhang), getResources().getDrawable(R.drawable.hejubao), getResources().getDrawable(R.drawable.hebaodai), getResources().getDrawable(R.drawable.chengchema), getResources().getDrawable(R.drawable.gongxiangchuxing), getResources().getDrawable(R.drawable.gongxiangchogndian), getResources().getDrawable(R.drawable.quabu)};
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(), 4);
        rl_hot.setLayoutManager(layoutManager);
        rl_hot.setItemAnimator(new DefaultItemAnimator());
        HomeHotAdapter homeHotAdapter = new HomeHotAdapter(getContext(), hotname, hotbg);
        rl_hot.setAdapter(homeHotAdapter);

        homeHotAdapter.setonRecycleViewClickListener(new HomeHotAdapter.onRecycleViewClickListener() {
            @Override
            public void onRecycleViewClick(int position) {

//                Toast.makeText(getContext(), "positon:" + position, Toast.LENGTH_SHORT).show();
                if (position == 8) {
                    Intent intent = new Intent(mContext, ChengcheActivity.class);
                    startActivity(intent);
                }
                if (position == 6 ||position ==7) {

                    checkPositionListener.changePosition(position);
                }


            }
        });
        initViewPagerData();
        initpoint();
        vp_advertisement.setAdapter(new BannerAdapter(getContext(), mlist));
        //监听页面的改变
        vp_advertisement.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                Log.i("LHD", "onPageScrolled" + "  positionOffset:" + positionOffset + "  positionOffsetPixels:" + positionOffsetPixels);
            }

            @Override
            public void onPageSelected(final int position) {
//                Log.i("LHD", "onPageSelected" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
//                Log.i("LHD", "onPageScrollStateChanged:" + state);
                if (state == 2) {//图片切换成功的状态为2
                    mPosition = vp_advertisement.getCurrentItem();
                    LogUtils.v("LHD当前页是：" + mPosition);
                    for (int i = 0; i < mlist.size(); i++) {
//设置所有原点
                        imgs[i].setImageResource(R.drawable.red_point);
                    }

                    //设置当前页的小圆点图片和文字信息
                    imgs[mPosition].setImageResource(R.drawable.blank_point);
                }
            }
        });

        imgPlay();
    }

    private void initextensionarea() {

        hotname1 = new String[]{"用户得20元，借款得100元", "查额成功，立返20", "签到享豪礼"};
        hotbg1 = new String[]{"白条ATM最高借20万 日息万1起", "无抵押 大额低息 秒到账", "连续签到七天送两次必中奖机会"};
        hotname1bg = new Drawable[]{getResources().getDrawable(R.drawable.yonghu), getResources().getDrawable(R.drawable.chae), getResources().getDrawable(R.drawable.qiandao)};

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        rl_extensionarea1.setLayoutManager(layoutManager);
        rl_extensionarea1.setItemAnimator(new DefaultItemAnimator());
        Extensionarea1Adapter homeHotAdapter = new Extensionarea1Adapter(getContext(), hotname1, hotbg1, hotname1bg);
        rl_extensionarea1.setAdapter(homeHotAdapter);

        hotname2 = new String[]{"视频会员有优惠", "电商购物省钱攻略"};
        hotbg2 = new String[]{"价格直降，五月嗨购", "卡券商城帮你省钱"};
        hotname2bg = new Drawable[]{getResources().getDrawable(R.drawable.shipinvip), getResources().getDrawable(R.drawable.shengqiangonglve)};
        RecyclerView.LayoutManager layoutManager2 = new GridLayoutManager(getContext(), 2);
        rl_extensionarea2.setLayoutManager(layoutManager2);
        rl_extensionarea2.setItemAnimator(new DefaultItemAnimator());
        Extensionarea2Adapter homeHotAdapte2 = new Extensionarea2Adapter(getContext(), hotname2, hotbg2, hotname2bg);
        rl_extensionarea2.setAdapter(homeHotAdapte2);

    }

    public void initViewPagerData() {

        mlist = new ArrayList<View>();

        ImageView iv = new ImageView(mContext);
        ImageView iv2 = new ImageView(mContext);
        ImageView iv3 = new ImageView(mContext);
        ImageView iv4 = new ImageView(mContext);
        ImageView iv5 = new ImageView(mContext);
        ImageView iv6 = new ImageView(mContext);

        iv.setImageResource(R.drawable.ad);
        iv2.setImageResource(R.drawable.ad1);
        iv3.setImageResource(R.drawable.ad);
        iv4.setImageResource(R.drawable.ad1);
        iv5.setImageResource(R.drawable.ad);
        iv6.setImageResource(R.drawable.ad1);

        mlist.add(iv);
        mlist.add(iv2);
        mlist.add(iv3);
        mlist.add(iv4);
        mlist.add(iv5);
        mlist.add(iv6);

        imgs = new ImageView[mlist.size()];

    }

    private LinearLayout point_container;

    private void initpoint() {

        for (int i = 0; i < mlist.size(); i++) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(R.drawable.blank_point);
            //对布局控件添加相对属性
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                    20,
                    20);
            //添加规则，示例 靠父控件最右边
            param.setMargins(30, 0, 0, 0);
            LogUtils.v("LHD添加小圆点：" + mlist.size());
            imgs[i] = imageView;//添加到图片数组
            point_container.addView(imageView, param);
            imgs[0].setImageResource(R.drawable.red_point);
        }
    }

    private void imgPlay() {
        //循环播放
        task = new TimerTask() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        };
        timer = new Timer();
        timer.schedule(task, 3000, 3000);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    if (mPosition == (mlist.size() - 1)) {
                        vp_advertisement.setCurrentItem(0, true);
                    } else {
                        LogUtils.v("LHDbbbb:   " + mPosition + "当前的进程ID：  " + Thread.currentThread().getId());
                        vp_advertisement.setCurrentItem(mPosition + 1, true);
                        LogUtils.v("LHDcccc:   " + mPosition);
                    }
                }
            }
        };
    }

    private void imgStop() {

        handler.removeMessages(1);
        timer.cancel();
        task.cancel();
    }


    @Override
    public void onDetach() {

        super.onDetach();
        imgStop();

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.img_title_xiaoxi:

//                Toast.makeText(getContext(),"点击事件",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), MessageActivity.class);
                startActivity(intent);
                break;
            case R.id.rb_cardbag:

//                Toast.makeText(getContext(),"点击事件",Toast.LENGTH_SHORT).show();
                Intent intent1 = new Intent(getContext(), KaquanbaoActivity.class);
                startActivity(intent1);

                break;
        }
    }

    CheckPositionListener checkPositionListener;

    public void setCheckPositionListener(CheckPositionListener checkPositionListener){

        this.checkPositionListener = checkPositionListener;
    }
    public interface  CheckPositionListener{

        public void changePosition(int positon);
    }
}
